package com.yq.testmodule.vp2

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ImmersionBar
import com.yq.testmodule.R
import com.yq.testmodule.view.AutoHeightImageView
import config.TEST_VIEWPAGER2
import net.lucode.hackware.magicindicator.buildins.UIUtil


@Route(path = TEST_VIEWPAGER2)
class TestViewpage2Activity : AppCompatActivity() {


    lateinit var mVp: ViewPager2
    lateinit var mAdapter: VPAdapter

    //    val datas = arrayListOf<VidListEntity>()
    var mCurrentPosition = -1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this).statusBarColor(R.color.black).statusBarDarkFont(false).init();
        setContentView(R.layout.test_activity_vp2)
        initData()
        mVp = findViewById(R.id.mVp)
        mAdapter = VPAdapter()
        mVp.adapter = mAdapter
        mVp.offscreenPageLimit = 2


        mVp.apply {
            val recyclerView = this.getChildAt(0) as RecyclerView // 获取ViewPager2中的 RecyclerView

            recyclerView.apply {
                val padding = UIUtil.dip2px(context, 40.0)
                setPadding(0, padding, 0, padding)
                clipToPadding = false
            }
        }

        mVp.setPageTransformer(ScaleInTransformer(0.65f, 0.85f))


    }

    val datas = mutableListOf<String>()
    fun initData() {
        datas.clear()
        datas.add("1")
        datas.add("2")
        datas.add("3")
        datas.add("4")
        datas.add("5")
    }


}


class VPAdapter() :
    RecyclerView.Adapter<VPAdapter.BaseViewHolder?>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val itemView: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.test_activity_vp2_item, parent, false)
        itemView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        return BaseViewHolder(itemView)
    }


    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val vp = holder.itemView.findViewById<ViewPager2>(R.id.vp)

        vp.adapter = ImageAdapter(
            listOf(

                "https://th.bing.com/th/id/R.d5761f06ce17ff70aca46bbb257d7723?rik=WbIc2YkQ0Rfp1w&pid=ImgRaw&r=0",
                "https://th.bing.com/th/id/OIP.5y5i1RJncmZmdmynd3GkBAHahb?rs=1&pid=ImgDetMain",
                "https://th.bing.com/th/id/R.9de53f9726576696b318a8d95c0946cb?rik=sWB3V9KSxHbThw&riu=http%3a%2f%2fpic.bizhi360.com%2fbbpic%2f95%2f9995_1.jpg&ehk=GcPUjJED69TBvg9XxQr2klzDzfRsQWhAfLKlIAUWHJQ%3d&risl=&pid=ImgRaw&r=0"
            )
        );
    }

    override fun getItemCount(): Int {
        return 20
    }

    inner class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {


    }


}

class ImageAdapter(var datas: List<String>) : RecyclerView.Adapter<ImageViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageViewHolder {
        val itemView: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.test_activity_vp2_item_rv_item, parent, false)
        return ImageViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
        val img = holder.itemView.findViewById<AutoHeightImageView>(R.id.iv)
        val scrollView = holder.itemView.findViewById<EventInterceptScrollView>(R.id.scrollview)
        scrollView.scrollTo(0, 0)
        Glide.with(holder.itemView.context)
            .load(datas[position])
            .into(img)

    }

}

class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {}